package com.tapstitch.scm.common.core.repository.condition;

import java.util.ArrayList;
import java.util.List;

/**
 * by geney
 * @param <T>
 */
public abstract class BaseCondition<T> {

    private String orderByClause;

    private boolean distinct;

    private List<T> oredCriteria;

    private int limitOffset = -1;

    private int limitSize = 1;

    public BaseCondition() {
        oredCriteria = new ArrayList<>();
    }

    public void setOrderByClause(String orderByClause) {
        this.orderByClause = orderByClause;
    }

    public String getOrderByClause() {
        return orderByClause;
    }

    public void setDistinct(boolean distinct) {
        this.distinct = distinct;
    }

    public boolean isDistinct() {
        return distinct;
    }

    public List<T> getOredCriteria() {
        return oredCriteria;
    }

    public int getLimitOffset() {
        return limitOffset;
    }

    public int getLimitSize() {
        return limitSize;
    }

    public void setLimitOffset(int limitOffset) {
        this.limitOffset = limitOffset;
    }

    public void setLimitSize(int limitSize) {
        this.limitSize = limitSize;
    }

    public void or(T criteria) {
        oredCriteria.add(criteria);
    }

    public T or() {
        T criteria = createCriteriaInternal();
        oredCriteria.add(criteria);
        return criteria;
    }

    public T createCriteria() {
        T criteria = createCriteriaInternal();
        if (oredCriteria.size() == 0) {
            oredCriteria.add(criteria);
        }
        return criteria;
    }

    /**
     * create CriteriaInternal
     *
     * @return
     */
    protected abstract T createCriteriaInternal();

    public void clear() {
        oredCriteria.clear();
        orderByClause = null;
        distinct = false;
    }

}
